package com.mf.morefun.service;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;

import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.bean.EventMessageBean;
import com.mf.morefun.bean.GrindingEarsBean;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FilenameFilter;
import java.util.List;

public class RecordMusicService {
    public MediaPlayer player; // 定义多媒体对象
    private String url;
    private Context mContext;
    public RecordMusicService(String url, Context mContext) {
        super();
        player = new MediaPlayer();
        this.url = url;
        this.mContext = mContext;
    }

    public void play(){
        try {
            if (null == player){
                player = new MediaPlayer();
            }
            player.reset(); //重置多媒体
            String dataSource = url;//得到当前播放音乐的路径
            // 指定参数为音频文件
            player.setAudioStreamType(AudioManager.STREAM_MUSIC);
            player.setDataSource(dataSource);//为多媒体对象设置播放路径
            player.prepare();//准备播放
            player.start();//开始播放
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                public void onCompletion(MediaPlayer arg0) {
                    EventMessageBean eventMessageBean = new EventMessageBean();
                    eventMessageBean.setNum(0);
                    eventMessageBean.setType(0);
                    eventMessageBean.setProgress(0);
                    EventBus.getDefault().post(eventMessageBean);
                }
            });
            player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    EventMessageBean eventMessageBean = new EventMessageBean();
                    eventMessageBean.setNum(mp.getDuration());
                    eventMessageBean.setType(2);
                    eventMessageBean.setProgress(0);
                    EventBus.getDefault().post(eventMessageBean);
                }
            });
            player.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    MoreFunApplication.isOut = true;
                    EventMessageBean eventMessageBean = new EventMessageBean();
                    eventMessageBean.setNum(0);
                    eventMessageBean.setType(1);
                    eventMessageBean.setProgress(0);
                    EventBus.getDefault().post(eventMessageBean);
                    return true;
                }
            });
        } catch (Exception e) {
            EventMessageBean eventMessageBean = new EventMessageBean();
            eventMessageBean.setNum(0);
            eventMessageBean.setType(1);
            eventMessageBean.setProgress(0);
            EventBus.getDefault().post(eventMessageBean);
        }
    }

    //继续播放
    public  void goPlay(){
        int position = getCurrentProgress();
        player.seekTo(position);//设置当前MediaPlayer的播放位置，单位是毫秒。
        player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(MediaPlayer mp) {
                try {
                    player.prepare();//  同步的方式装载流媒体文件。
                } catch (Exception e) {
                    e.printStackTrace();
                }
                player.start();
            }
        });
    }

    // 获取当前进度
    public int getCurrentProgress() {
        if (player != null & player.isPlaying()) {
            return player.getCurrentPosition();
        } else if (player != null & (!player.isPlaying())) {
            return player.getCurrentPosition();
        }
        return 0;
    }

    // 暂停播放
    public void pause() {
        if (player != null && player.isPlaying()){
            player.pause();
        }
    }

    public void stop() {
        if (player != null && player.isPlaying()) {
            player.stop();
            player.reset();
            player.release();
            player = null;
        }
    }
}


